$(document).ready(function() {
	$.getJSON("../Bookmarks.txt", function(result) {
		parse(result);
		$("div").html("parse finished!");
	});
});

var bookmarks = new Array;
// var folderNames = new Array;
var sources = new Array;
var path = new Array;
var tabs = new Array;
tabs.push("\t");

var txt = "";

function parse(rootObject) {
	$.each(rootObject.roots, function(i, field) {
		parse_object(field);
	});
}

function parse_array(array) {
	for (var i = 0; i < array.length; i++) {
		var object = array[i];
		parse_object(object);
	};
}

function parse_object(object) {
	if (object.type == "folder") {
		if ($.type(object.children) == "array") {
			
			path.push(object.name);
			parse_array(object.children);
			path.pop();
			
		} else {
			console.log("unexcepted");
		}
	} else if (object.type == "url") {
		analyse_item(object);
	} else {
		console.log("unexcepted");
	}
}


var rooturis = new Object;
rooturis.normal = new Object;
rooturis.secure = new Object;
rooturis.count = 0;

function analyse_item(item) {
	var uri = item.url;
	var rooturi = uri.match(/http:\/\/.*?\//);
	
	/*
	var array = null;
	if(undefined != rooturi){
		array = rooturis.normal[rooturi];
	}
	*/
	if (undefined == rooturi) {
		var rooturi = uri.match(/https:\/\/.*?\//);
	}
	if (undefined != rooturi) {
		var array = rooturis[rooturi];
		if (undefined == array) {
			array = [];
			rooturis[rooturi] = array;
			rooturis.count++;
		}
		array.push(uri);
	}
	return rooturi;
}
